package com.health.focusgroup;

import java.util.ArrayList;

import com.health.NovaFragmentActivity;
import com.health.PersonalFileActivity.onFragmentSave;
import com.health.R;
import com.health.adapter.FragmentAdapter;
import com.health.fragment.focusgroup.maternity.Maternity_BaseFragment;
import com.health.fragment.focusgroup.maternity.Maternity_Ch42Fragment;
import com.health.fragment.focusgroup.maternity.Maternity_ChfsFragment;
import com.health.fragment.focusgroup.maternity.Maternity_Cqsf1Fragment;
import com.health.fragment.focusgroup.maternity.Maternity_Cqsf2Fragment;
import com.health.fragment.focusgroup.maternity.Maternity_Cqsf3Fragment;
import com.health.fragment.focusgroup.maternity.Maternity_Cqsf4Fragment;
import com.health.fragment.focusgroup.maternity.Maternity_Cqsf5Fragment;
import com.health.tools.GlobalConts;
import com.novabluetooth.views.ExamHistoryActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

/**
 * 重点人群--孕妇
 * 
 * @author nova
 *
 */
public class MaternityActivity extends NovaFragmentActivity implements OnClickListener {

	private ViewPager viewPager;
	private ArrayList<RadioButton> radioButtons;
	private ArrayList<Fragment> fragments;

	private TextView title;
	private Button btn_back;
	public static Button btn_up;
	FragmentAdapter adapter = null;
	public static Activity mMaternityActivity = null;
	private Fragment cqsf1Fragment;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		// getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
		// WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.maternity_main);
		initView();
		addListener();
	}

	private void initView() {
		title = (TextView) findViewById(R.id.title);
		title.setText("重点人群--孕妇");

		btn_back = (Button) findViewById(R.id.btn_back);
		btn_up = (Button) findViewById(R.id.btn_up);
		
		mMaternityActivity = this;

		viewPager = (ViewPager) findViewById(R.id.viewpager);
		RadioButton maternity_base = (RadioButton) findViewById(R.id.maternity_base);
		RadioButton maternity_cqsf1 = (RadioButton) findViewById(R.id.maternity_cqsf1);
		RadioButton maternity_cqsf2 = (RadioButton) findViewById(R.id.maternity_cqsf2);
		RadioButton maternity_cqsf3 = (RadioButton) findViewById(R.id.maternity_cqsf3);
		RadioButton maternity_cqsf4 = (RadioButton) findViewById(R.id.maternity_cqsf4);
		RadioButton maternity_cqsf5 = (RadioButton) findViewById(R.id.maternity_cqsf5);
		RadioButton maternity_chfs = (RadioButton) findViewById(R.id.maternity_chfs);
		RadioButton maternity_ch42 = (RadioButton) findViewById(R.id.maternity_ch42);

		radioButtons = new ArrayList<RadioButton>();
		radioButtons.add(maternity_base);
		radioButtons.add(maternity_cqsf1);
		radioButtons.add(maternity_cqsf2);
		radioButtons.add(maternity_cqsf3);
		radioButtons.add(maternity_cqsf4);
		radioButtons.add(maternity_cqsf5);
		radioButtons.add(maternity_chfs);
		radioButtons.add(maternity_ch42);

		maternity_base.setOnClickListener(this);
		maternity_cqsf1.setOnClickListener(this);
		maternity_cqsf2.setOnClickListener(this);
		maternity_cqsf3.setOnClickListener(this);
		maternity_cqsf4.setOnClickListener(this);
		maternity_cqsf5.setOnClickListener(this);
		maternity_chfs.setOnClickListener(this);
		maternity_ch42.setOnClickListener(this);

		Fragment baseFragement = new Maternity_BaseFragment();
		cqsf1Fragment  = new Maternity_Cqsf1Fragment();
		Fragment cqsf2Fragment = new Maternity_Cqsf2Fragment();
		Fragment cqsf3Fragment = new Maternity_Cqsf3Fragment();
		Fragment cqsf4Fragment = new Maternity_Cqsf4Fragment();
		Fragment cqsf5Fragment = new Maternity_Cqsf5Fragment();
		Fragment chfsFragment = new Maternity_ChfsFragment();
		Fragment ch42Fragment = new Maternity_Ch42Fragment();
		fragments = new ArrayList<Fragment>();
		fragments.add(baseFragement);
		fragments.add(cqsf1Fragment);
		fragments.add(cqsf2Fragment);
		fragments.add(cqsf3Fragment);
		fragments.add(cqsf4Fragment);
		fragments.add(cqsf5Fragment);
		fragments.add(chfsFragment);
		fragments.add(ch42Fragment);

		adapter = new FragmentAdapter(getSupportFragmentManager(), fragments);
		viewPager.setAdapter(adapter);
		title.setText("孕妇  —— 基本信息");
		setChecked(0);
	}

	private void addListener() {

		viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

			}

			@Override
			public void onPageSelected(int position) {
				radioButtons.get(position).setChecked(true);
				setChecked(position);
			}

			@Override
			public void onPageScrollStateChanged(int state) {

			}
		});

		btn_back.setOnClickListener(this);
		btn_up.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {

		int id = v.getId();
		if (id == R.id.maternity_base) {
			viewPager.setCurrentItem(0);
			setChecked(0);
		} else if (id == R.id.maternity_cqsf1) {
			viewPager.setCurrentItem(1);
			setChecked(1);
		} else if (id == R.id.maternity_cqsf2) {
			viewPager.setCurrentItem(2);
			setChecked(2);
		} else if (id == R.id.maternity_cqsf3) {
			viewPager.setCurrentItem(3);
			setChecked(3);
		} else if (id == R.id.maternity_cqsf4) {
			viewPager.setCurrentItem(4);
			setChecked(4);
		} else if (id == R.id.maternity_cqsf5) {
			viewPager.setCurrentItem(5);
			setChecked(5);
		} else if (id == R.id.maternity_chfs) {
			viewPager.setCurrentItem(6);
			setChecked(6);
		} else if (id == R.id.maternity_ch42) {
			viewPager.setCurrentItem(7);
			setChecked(7);
		} else if (id == R.id.btn_back) {
			this.finish();
		} else if (id == R.id.btn_up) {
			// 保存
			onFragmentSave currentFragment = (onFragmentSave) adapter.getCurrentFragment();
			currentFragment.save();
		} else {
		}

	}
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		onFragmentSave fragment = (onFragmentSave) adapter.getCurrentFragment();
		if (requestCode == GlobalConts.RequestXueya && resultCode == ExamHistoryActivity.ResultXueya) {
			fragment.setXueya(data.getStringExtra("高压"), data.getStringExtra("低压"), data.getStringExtra("脉率"));
		} else if (requestCode == GlobalConts.RequestTizhong && resultCode == ExamHistoryActivity.ResultTizhong) {
			fragment.setTizhong(data.getStringExtra("value"));
		} else if (requestCode == GlobalConts.RequestTiwen && resultCode == ExamHistoryActivity.ResultTiwen) {
			Log.w("eeeeeeeee", data.getStringExtra("value"));
			fragment.setTiWen(data.getStringExtra("value"));
		}  else if (requestCode == GlobalConts.RequestXueHongDanbai && resultCode == ExamHistoryActivity.ResultXueHongDanbai) {
			Log.w("eeeeeeeee", data.getStringExtra("value"));
			fragment.setXueHongDanBai(data.getStringExtra("value"));
		} else if (requestCode == GlobalConts.RequestNiaoChangGui && resultCode == ExamHistoryActivity.ResultNiaoChangGui) {
			fragment.setNiaoYeFenxi(data.getStringExtra("尿胆原"), data.getStringExtra("潜血"), data.getStringExtra("胆红素"), data.getStringExtra("酮体"), data.getStringExtra("葡萄糖"), data.getStringExtra("蛋白质"), data.getStringExtra("PH"), data.getStringExtra("亚硝酸盐"), data.getStringExtra("白细胞"), data.getStringExtra("比重"), data.getStringExtra("维生素"));
		} else if (requestCode == GlobalConts.RequestXuetang && resultCode == ExamHistoryActivity.ResultXuetang) {
			Log.w("eeeeeeeee", data.getStringExtra("value"));
			fragment.setXuetang(data.getStringExtra("value"));
		} 
	}
	@SuppressLint("ResourceAsColor")
	private void setChecked(int pos) {
		for (RadioButton rbtn : radioButtons) {
			rbtn.setBackground(null);
		}
		radioButtons.get(pos).setBackgroundResource(R.drawable.rd_chek_center);
		if(pos==0){
			title.setText("孕妇  —— 基本信息");
		}else if(pos==1){
			title.setText("孕妇  —— 产前随访1");
			((Maternity_Cqsf1Fragment) cqsf1Fragment).setHusbandInfo();
		}else if(pos==2){
			title.setText("孕妇  —— 产前随访2");
		}else if(pos==3){
			title.setText("孕妇  —— 产前随访3");
		}else if(pos==4){
			title.setText("孕妇  —— 产前随访4");
		}else if(pos==5){
			title.setText("孕妇  —— 产前随访5");
		}else if(pos==6){
			title.setText("孕妇  —— 产后访视");
		}else if(pos==7){
			title.setText("孕妇  —— 产后42天");
		}
	
	}
}
